<!DOCTYPE html>
<html>

<head>
    <title>扫雷</title>
    <style>
        div {
            background-color: black;
            width: 30px;
            height: 30px;
            border: 1px solid #080;
            display: inline-block;
            margin: 0.5px;
            text-align: center;
            line-height: 30px;
            cursor: default;
        }
    </style>
</head>

<body>
    <h1 id="heading">扫雷</h1>








    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>

    <script>
        var h = 10
        var w = 15
        var da = []
        for (i = 0; i < h; i++) {

            var xiao = []
            for (j = 0; j < w; j++) {
                xiao.push(" ")



            }
            da.push(xiao)

        }


        var n = 20
        for (k = 0; k < n; k++) {
            var x = Math.floor(Math.random() * w)
            var y = Math.floor(Math.random() * h)
            var b = da[y][x]
            if (b !== "x") {
                da[y][x] = "x"
            } else {
                k--
            }
        }


        for (y = 0; y < h; y++) {
            for (x = 0; x < w; x++) {
                var a = da[y][x]
                if (a !== "x") {
                    var n = 0 // n 表示雷的数量
                    for (y1 = y - 1; y1 <= y + 1; y1++) {
                        for (x1 = x - 1; x1 <= x + 1; x1++) {
                            if (x1 >= 0 && x1 < w) {
                                if (y1 >= 0 && y1 < h) {
                                    if (da[y1][x1] === "x") {
                                        n++
                                    }
                                }

                            }
                        }
                    }
                    if (n !== 0) { da[y][x] = n.toString() } else { da[y][x] = "　" }
                }
            }

        }
        for (y2 = 0; y2 < h; y2++) {
            for (x2 = 0; x2 < w; x2++) {
                var c = "　"//da[y2][x2]
                $("body").append("<div x=" + x2 + " y=" + y2 + " >" + c + "</div>")
            }
            $("body").append("<br>")
        }

        var opened = [] //已经
        var plan = []  //计划
        var flag1 = []//雷
        var flag2 = []//？


        var dj0 = function (e) {

            //console.log(e, e.length)

            if (e.length === 2) {
                var x = e[0]
                var y = e[1]
                var s = "body [x=" + x + "][y=" + y + "]"
                var d = $(s)[0]
                console.log(d)
                d.style.backgroundColor = "yellow"

                $(s).text(da[y][x])

            } else {
                var x = parseInt(e.target.attributes["x"].value)
                var y = parseInt(e.target.attributes["y"].value)
                e.target.style.backgroundColor = "yellow"
                e.target.innerHTML = da[y][x]

            }

            var v = da[y][x]

            opened.push([x, y])

            if (v === "　") {

                for (y1 = y - 1; y1 <= y + 1; y1++) {
                    for (x1 = x - 1; x1 <= x + 1; x1++) {
                        if (x1 >= 0 && x1 < w) {
                            if (y1 >= 0 && y1 < h) {


                                var zaiBuZai = false
                                for (n = 0; n < plan.length; n++) {
                                    var t = plan[n]
                                    if (x1 === t[0] && y1 === t[1]) {
                                        zaiBuZai = true
                                        break;
                                    }
                                }
                                if (zaiBuZai === false) {
                                    for (n = 0; n < opened.length; n++) {
                                        var t = opened[n]
                                        if (x1 === t[0] && y1 === t[1]) {
                                            zaiBuZai = true
                                            break;
                                        }
                                    }
                                }
                                if (zaiBuZai === false) {
                                    plan.push([x1, y1])
                                }


                            }

                        }
                    }
                }
                //console.log(plan.length)



                while (plan.length > 0) {
                    var p = plan.pop()
                    //console.log(p)
                    dj0(p)
                }

                // for (i = 0; i < plan.length; i++) {
                //     var p = plan[i]
                //     console.log(p)
                //     func(p)
                // }


            } else if (v === "x") {

                for (y = 0; y < h; y++) {
                    for (x = 0; x < w; x++) {
                        var c = da[y][x]
                        if (c === "x") {
                            var s = "body [x=" + x + "][y=" + y + "]"
                            var d = $(s)[0]
                            d.style.backgroundColor = "green"



                            e.target.style.backgroundColor = "red"

                            var zaiBuZai=false
                            for (n = 0; n < flag1.length; n++){
                                var t = flag1[n]
                                if (x === t[0] && y === t[1]) {
                                    zaiBuZai=true
                                    break
                                }
                            }
                            if (zaiBuZai===false){
                                d.innerHTML=c
                            }


                            // console.log(x,y,t)
                            // for (n = 0; n < flag1.length; n++) {
                            //     var t = flag1[n]
                            //     console.log(x,y,t)
                            //     if (x === t[0] && y === t[1]) {
                            //         console.log("p")
                            //         break

                            //     }
                            //     else
                            //     {   
                            //          d.innerHTML=c
                            //         console.log("x")
                            //         break 
                            //     }
                            // }
                        }
                    }
                }
            }
        }
        // $("div").click(func)

        var dj2 = function (e) {
            console.log(e)
            if (e.button === 2) {

                var x = parseInt(e.target.attributes["x"].value)
                var y = parseInt(e.target.attributes["y"].value)
                var zaiBuZai = false
                for (n = 0; n < opened.length; n++) {
                    var t = opened[n]
                    if (x === t[0] && y === t[1]) {
                        zaiBuZai = true
                        break;
                    }
                }
                if (zaiBuZai === false) {

                    for (n = 0; n < flag1.length; n++) {
                        var t = flag1[n]
                        if (x === t[0] && y === t[1]) {
                            zaiBuZai = true
                            break;
                        }
                    }
                    if (zaiBuZai === true) {
                        flag1.splice(n, 1)
                        flag2.push([x, y])
                        //显示？
                        e.target.style.backgroundColor = "blue"
                        e.target.innerHTML = "?"
                        console.log("?")



                    } else {
                        var zaiBuZai = false

                        for (n = 0; n < flag2.length; n++) {
                            var t = flag2[n]
                            if (x === t[0] && y === t[1]) {
                                zaiBuZai = true
                                break;
                            }
                        }
                        console.log(flag2, zaiBuZai)
                        if (zaiBuZai === false) {

                            flag1.push([x, y])
                            //显示qiqi
                            e.target.style.backgroundColor = "blue"
                            e.target.innerHTML = "P"
                            console.log("P")


                        } else {
                            flag2.splice(n, 1)
                            //显示为空白
                            e.target.style.backgroundColor = "black"
                            e.target.innerHTML = "　"
                            console.log(" ")
                        }

                    }




                }
            }


        }












        // $("div").mousedown(md)
        $("div").contextmenu(function (e) { e.stopPropagation(); e.preventDefault() })


        var m0 = false, m2 = false, sfsj = false, zt = 0


        var newMD = function (e) {
            zt++
            if (e.button === 0) {
                m0 = true
                if (m2 === true) {
                    //sjdm
                    sj(e)
                    sfsj = true
                } else { sfsj = false }
            } else if (e.button === 2) {
                m2 = true
                if (m0 === true) {
                    //sjdm   
                    sj(e)
                    sfsj = true
                } else { sfsj = false }
            }
        }
        $("div").mousedown(newMD)

        var mu = function (e) {

            zt--
            if (e.button === 0) {
                m0 = false
                if (sfsj === false) {
                    //dj
                    dj0(e)
                }
            } else if (e.button === 2) {
                m2 = false
                if (sfsj === false) {
                    //dj
                    dj2(e)
                }
            }
        }
        $("div").mouseup(mu)


        var sj = function (e) {

            var x = parseInt(e.target.attributes["x"].value)
            var y = parseInt(e.target.attributes["y"].value)
            var v = da[y][x]
            var zaiBuZai = false
            for (n = 0; n < opened.length; n++) {
                var t = opened[n]
                if (x === t[0] && y === t[1]) {
                    zaiBuZai = true
                    break;
                }
            }
            if (zaiBuZai === true) {
                if (v !== "　" && v !== "x") {
                    console.log(2222)
                    var a = 0

                    for (y1 = y - 1; y1 <= y + 1; y1++) {
                        for (x1 = x - 1; x1 <= x + 1; x1++) {
                            if (x1 >= 0 && x1 < w) {
                                if (y1 >= 0 && y1 < h) {

                                    for (n = 0; n < flag1.length; n++) {
                                        var t = flag1[n]
                                        if (x1 === t[0] && y1 === t[1]) {
                                            a++
                                            break;
                                        }
                                    }

                                }
                            }
                        }
                    }
                    console.log(a, v)
                    if (a.toString() === v) {

                        for (y1 = y - 1; y1 <= y + 1; y1++) {
                            for (x1 = x - 1; x1 <= x + 1; x1++) {
                                if (x1 >= 0 && x1 < w) {
                                    if (y1 >= 0 && y1 < h) {

                                        var zaiBuZai = false
                                        for (n = 0; n < flag1.length; n++) {
                                            var t = flag1[n]
                                            if (x1 === t[0] && y1 === t[1]) {
                                                zaiBuZai = true
                                                break;
                                            }
                                        }
                                        if (zaiBuZai === false) {
                                            for (n = 0; n < opened.length; n++) {
                                                var t = opened[n]
                                                if (x1 === t[0] && y1 === t[1]) {
                                                    zaiBuZai = true
                                                    break;
                                                }
                                            }
                                        }
                                        if (zaiBuZai === false) {
                                            dj0([x1, y1])
                                        }


                                    }
                                }
                            }
                        }


                    }





                }
            }


        }

    </script>
</body>

</html>